package com.share.example.json2flat.web;

import com.alibaba.fastjson.JSONObject;
import com.share.common.common.Result;
import com.share.example.json2flat.service.HbaseService;
import com.share.example.json2flat.service.IMappingConfService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author author
 * @since 2024-05-08
 */
@RestController
public class CommonController {

    @Autowired
    private IMappingConfService mappingConfService;

    @Autowired
    private HbaseService hbaseService;

    //发送消息到flat所在的topic
    @RequestMapping("/send/flatJson")
    public Result sendMsg2FlatJsonKafka(@RequestBody JSONObject object) {
        Integer integer = mappingConfService.sendMsg2FlatJsonKafka(object);
        return Result.insertOrDeleteOrUpdate(integer);
    }

    //重新加载配置
    @RequestMapping("/reload/conf")
    public Result reloadConf() {
        Object init = mappingConfService.init();
        return Result.success(init);
    }

    @ApiOperation("清理删除表中的数据,保留最近的n条")
    @ResponseBody
    @RequestMapping(value = "/common/deleteLog")
    public Result deleteLog(Integer retainNum) {
        try {
            Map<String, Object> reMap = hbaseService.deleteLog(retainNum);
            return Result.success(reMap);
        } catch (Exception e) {
            return Result.failure(e.getMessage());
        }
    }
}
